<?php
/**
 * Created by PhpStorm.
 * User: keke
 * Date: 2020/8/19
 * Time: 18:21
 */

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;

class UnionParam extends Base
{
    protected $table = 'union_param';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }


    /**
     * 自动设置-创建[每日预支/自定义预支金额/指定高管预支/每日提现/指定高管提现]开关
     * @param $advanceArr [包含了对应的公会Id]
     * @return bool
     */
    public function createAutomaticParam($advanceArr)
    {
        $result = DB::table($this->table)->insert($advanceArr);
        return $result;
    }


    /**
     * 自动设置[每日预支/自定义预支金额/指定高管预支/每日提现/指定高管提现]开关
     * @param $unionId
     * @param $advanceArr
     * @return int
     */
    public function setAutomaticParam($unionId, $advanceArr)
    {
        $dailyAdvance = $advanceArr['a_daily_advance'];
        $selfAdvance = $advanceArr['a_self_advance'];
        $setAdvance = $advanceArr['a_set_advance'];
        $dailyWithdraw = $advanceArr['a_daily_withdraw'];
        $setWithdraw = $advanceArr['a_set_withdraw'];

        $nowTime = date('Y-m-d H:i:s');
        return DB::INSERT("INSERT INTO zb_union_param (`union_id`,`a_daily_advance`,`a_self_advance`,`a_set_advance`,`a_daily_withdraw`,`a_set_withdraw`,`created_at`) VALUE ({$unionId}, {$dailyAdvance}, {$selfAdvance}, {$setAdvance}, {$dailyWithdraw}, {$setWithdraw}, '{$nowTime}') ON DUPLICATE KEY UPDATE a_daily_advance = {$dailyAdvance}, a_self_advance = {$selfAdvance}, a_set_advance = {$setAdvance}, a_daily_withdraw = {$dailyWithdraw}, a_set_withdraw = {$setWithdraw}, updated_at = '{$nowTime}'");
    }

    /**
     * 根据公会ID获取相应权限
     * @param $unionId
     * @return array
     */
    public function getParams($unionId)
    {
        if (is_array($unionId)) {
            $result = DB::table($this->table)->whereIn('union_id', $unionId)->get();
        } else {
            $result = DB::table($this->table)->where(['union_id' => $unionId])->first();
        }

        if (empty($result)) {
            return [];
        }
        return $this->objToArray($result);
    }
}